<?php
/**
 * @file $HeadURL: job.php $
 * @author $Author: ZhangFan $
 * @date $Date: 2015-12-14 $
 * @brief
 */

namespace  haibao\cms\business;

use \haibao\cms\data\Job as JobData;
use \haibao\cms\model\filter\job as JobFilter;
use \haibao\frame\data\query\Condition;
use haibao\app\business\BusinessException;

class Job extends \haibao\cms\business\Base{
    public static function checkFields($model){
        if(empty($model->Title)){
            throw new BusinessException('请添加岗位名称');
        }
        if(empty($model->CateId)){
            throw new BusinessException('请选择岗位类型');
        }
        if(empty($model->JobArea)){
            throw new BusinessException('请添加工作地点');
        }
        if(empty($model->Num)){
            throw new BusinessException('请添加招聘人数');
        }
        if(empty($model->JobDesc)){
            throw new BusinessException('请添加岗位描述');
        }
        if(empty($model->JobRequire)){
            throw new BusinessException('请添加岗位要求');
        }
        if(empty($model->StartTime)){
            throw new BusinessException('请添加招聘开始时间');
        }
        if(empty($model->EndTime)){
            throw new BusinessException('请添加招聘截止时间');
        }
    }
    public static function addJob($model){
        self::checkOperation(self::FUNC_CODE_JOB_ADD);
        self::checkFields($model);
        $jobData = new JobData();
        $jobData->add($model);
        
        self::saveOperateHistory(self::FUNC_CODE_JOB_ADD,$model->toArray(false));
    }
    
    public static function getJobById($jobId){
        if(!$jobId){
            throw new BusinessException('岗位ID不能为空');
        }
        $jobData = new JobData();
        return $jobData->getOneById($jobId);
    }
    
    public static function getJobList($jobFilter){
        $jobData = new JobData();
        return $jobData->getAll($jobFilter);
    }
    public static function updateJob($model){
        self::checkOperation(self::FUNC_CODE_JOB_EDIT);
        self::checkFields($model);
        $jobData = new JobData();
        $jobData->updateById($model->Id, $model);
        self::saveOperateHistory(self::FUNC_CODE_JOB_EDIT, $model->toArray(false));
    }
    public static function changeJobStatus($id, $status){
        self::checkOperation(self::FUNC_CODE_JOB_DEL);
        if(!$id){
            throw new BusinessException('岗位Id不能为空');
        }
        $jobData = new JobData();
        $jobData->changeJobStatusById($id, $status);
        self::saveOperateHistory(self::FUNC_CODE_JOB_DEL, array($id,$status));
    }
    public static function getJobCateStatus($id){
        $jobData = new JobData();
        return $jobData->getJobCateStatus($id);
    }
    public static function getAllCates($model){
        $data = new JobData();
        
        return $data->getAll($model);
    }
    
    public static function hasSameJobName($title,$jobId = NULL){
        $hased = false;
        $jobFilter = new JobFilter();
        if(!$title){
            throw new BusinessException('岗位名称不能为空');
        }
        if($title){
            $jobFilter->where(JobFilter::CONDITION_FIELD_NAME_TITLE, Condition::CONDITION_EQUAL, $title);
        }
        $jobFilter->select(array(
            JobFilter::CONDITION_FIELD_NAME_TITLE,
            JobFilter::CONDITION_FIELD_NAME_ID
        ));
        $jobData = new JobData();
        $jobInfo = $jobData->getOne($jobFilter);
        if($jobInfo && $jobInfo->Id != $jobId){
            $hased = true;
        }
        return $hased;
    }
}